package com.webmall.servlet.cart;

import com.webmall.entity.CartEntity;
import com.webmall.entity.ProdEntity;
import com.webmall.service.webmall_CartDao;
import com.webmall.service.webmall_ProdDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;

@WebServlet("/orderSelect")
public class orderSelect extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();
        String isLogin = String.valueOf(session.getAttribute("isLogin"));


        if (!isLogin.equals("1")){
            PrintWriter out = response.getWriter();
            out.write("<script>");
            out.write("alert('请先登录！');");
            out.write("location.href='login.jsp';");
            out.write("</script>");
            out.close();
        }else {
            String ids =request.getParameter("cartId");
            String[] id = ids.split(",");
            ArrayList<CartEntity> carts = new ArrayList<>();
            int totalPrice =0;
            for (int i=0;i<id.length;i++){
                CartEntity cart= webmall_CartDao.selectByCARTId(id[i]);
                totalPrice += Integer.valueOf(cart.getCART_P_PRICE())*Integer.valueOf(cart.getCART_MOUNT());//传递总价
                carts.add(cart);

            }

            request.setAttribute("cartsList",carts);
            request.setAttribute("totalPrice",totalPrice);
            request.getRequestDispatcher("order.jsp").forward(request,response);



        }
    }
}
